﻿using Devil;
using GameToolkit.UI;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UI;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(RichText), true)]
    [CanEditMultipleObjects]
    public class RichTextInspector : GraphicEditor
    {
        SerializedProperty m_Text;
        SerializedProperty m_FontData;
        SerializedProperty m_DefaultAtlas;
        SerializedProperty m_Anims;
        SerializedProperty m_DefaultAtlasFolder;
        SerializedProperty m_SupportEmoji;
        SerializedProperty m_FixSymbolLevel;

        List<RichText.EmojiButton> btns;

        protected override void OnEnable()
        {
            base.OnEnable();
            bool hasfield;
            m_Text = serializedObject.FindProperty("m_Text");
            m_FontData = serializedObject.FindProperty("m_FontData");
            m_DefaultAtlas = serializedObject.FindProperty("m_DefaultAtlas");
            m_Anims = serializedObject.FindProperty("m_Anims");
            m_SupportEmoji = serializedObject.FindProperty("m_SupportEmoji");
            m_FixSymbolLevel = serializedObject.FindProperty("m_FixSymbolLevel");
            m_DefaultAtlasFolder = serializedObject.FindProperty("m_DefaultAtlasFolder");
            btns = Ref.GetField(target, "mEmojiBtns", out hasfield) as List<RichText.EmojiButton>;
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.PropertyField(m_Text);
            EditorGUILayout.PropertyField(m_FontData);
            EditorGUILayout.PropertyField(m_Anims);
            EditorGUILayout.PropertyField(m_SupportEmoji);
            EditorGUILayout.PropertyField(m_FixSymbolLevel);
            EditorGUILayout.PropertyField(m_DefaultAtlasFolder);
            EditorGUILayout.PropertyField(m_DefaultAtlas);

            AppearanceControlsGUI();
            RaycastControlsGUI();
            MaskableControlsGUI();
            serializedObject.ApplyModifiedProperties();
        }

        private void OnSceneGUI()
        {
            if (btns != null)
            {
                var trans = ((RichText)target).transform;
                Handles.matrix = trans.localToWorldMatrix;
                for (int i = 0; i < btns.Count; i++)
                {
                    var btn = btns[i];
                    Handles.Label(btn.rect.center,
                        ParallelUtils.Concat("Btn_", btn.clickId), "box");
                }
            }
        }

    }
}